package com.xx.douyi.service.impl;

import cn.hutool.core.date.DateUtil;
import com.xx.douyi.bean.GPGGA;
import com.xx.douyi.service.GPGGAService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: com.xx.douyi.service.impl
 * @Author: ruby
 * @CreateTime: 2023-04-19 12:35
 */
@Service
public class GPGGAServiceImpl implements GPGGAService {
    @Override
    public GPGGA transform(String data) {
        String regex = "(\\d{2})(\\d{2})(\\d{2})\\.?(\\d+)?,(\\d{2})(\\d{2}\\.\\d+),([NS]),(\\d{3})(\\d{2}\\.\\d+),([EW]),([0-8]),(\\d{1,2}),(\\d{1,3}(?:\\.\\d+)?),(-?|-?\\d+(?:\\.\\d+)?),[M-]?,(-?|-?\\d+(?:\\.\\d+)?),[M-]?,(-?|\\d+(?:\\.\\d+)?),(\\d{4}|\\w{4})?,?([ADEMSNP])?";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(data);
        GPGGA gpgga = null;
        if (matcher.find()) {
            System.out.println("Full match: " + matcher.group(0));
            gpgga = new GPGGA();
            gpgga.setTime(getDateFromTime(matcher.group(1),matcher.group(2),matcher.group(3),matcher.group(4)));
            gpgga.setLat(getLat(matcher.group(5),matcher.group(6),matcher.group(7)));
            gpgga.setLon(getLon(matcher.group(8),matcher.group(9),matcher.group(10)));
            gpgga.setUseNowNum(Integer.valueOf(matcher.group(12)));
            gpgga.setHDOPaccuracy(Double.parseDouble(matcher.group(13)));
            gpgga.setAltitude(Double.parseDouble(matcher.group(14)));
        }
        return gpgga;

    }

    public String getDateFromTime(String h, String m, String s, String ms){
        String utcTime = String.format("%s:%s:%s:%s",h,m,s,ms);
        String format = DateUtil.format(new Date(), "yyyy-MM-dd");
//        Date date = new Date(format + "T" + utcTime + "Z");
        return utcTime;
    }
    public String getLat(String d, String m, String dir){
        String n = String.valueOf(((Integer.valueOf(d) + (Double.parseDouble(m)/60.0)) * (dir.equals("N") ? 1 : -1)));
        return n;
    }
    public String getLon(String d, String m, String dir){
        String n = String.valueOf(((Integer.valueOf(d) + (Double.parseDouble(m)/60.0)) * (dir.equals("E") ? 1 : -1)));
        return n;
    }
}
